<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { addMenu, getOrgAll } from '@/api/syssetting';
  import { FormInstance, Message } from '@arco-design/web-vue';
  import useLoading from '@/hooks/loading';

  const formRef = ref<FormInstance | null>(null);
  const props = defineProps({
    visible: Boolean,
    params: Object,
    formType: {
      type: Array,
      default: () => [], // 这里默认值建议返回空数组，而不是空对象
    },
  });
  const { loading, setLoading } = useLoading();
  const isShow = ref(true);
  defineOptions({
    name: 'userAddSetting',
  });

  const id = props.params?.id ? props.params.id : '';
  const formData = ref({
    menu_parent_id: id, //父级菜单id
    menu_type: 1, //菜单类型 1-目录 2-菜单 3-按钮
    menu_name: '', //菜单名称
    menu_icon: '', //菜单图标
    menu_route_path: '', //路由路径
    menu_component_path: '', //组件路径
    menu_route_params: '', //路由参数
    menu_show: 1, //是否显示 1-显示 2-隐藏
    menu_state: 1, //菜单状态 1-正常 2-禁用
    menu_sort: '', //菜单排序
    menu_auth: '', // 权限标识
  });
  const emit = defineEmits(['update:visible', 'refresh']);

  const handleSubmit = async () => {
    if (!(await formRef.value?.checkForm())) {
      try {
        setLoading(true);
        const res = await addMenu(formRef.value?.formModel);
        Message.success(res.msg);
        emit('update:visible', false);
        setLoading(false);
        emit('refresh');
      } catch (error) {
        setLoading(false);
      }
    } else {
      Message.error('请正确填写表单！');
    }
  };

  // 创建一个新的响应式变量来存储处理后的 formType
  const processedFormType = ref(props.formType);

  // 过滤函数
  const filterFormType = (formTypeArray: any, type: number) => {
    isShow.value = false;
    if (type == 1) {
      isShow.value = true;
      return formTypeArray.filter(
        (item: any) =>
          item.name !== 'menu_component_path' &&
          item.name !== 'menu_route_params' &&
          item.name !== 'menu_auth'
      );
    } else if (type == 2) {
      isShow.value = true;
      return formTypeArray;
    } else if (type == 3) {
      isShow.value = true;
      return formTypeArray.filter(
        (item: any) =>
          item.name !== 'menu_component_path' &&
          item.name !== 'menu_route_params' &&
          item.name !== 'menu_icon' &&
          item.name !== 'menu_route_path' &&
          item.name !== 'menu_component_path' &&
          item.name !== 'menu_route_params' &&
          item.name !== 'menu_show' &&
          item.name !== 'menu_state'
      );
    }
  };

  // 处理后的 formType，传递给子组件
  const filteredFormType = ref(filterFormType(props.formType, 1));

  const handleFormModelChange = (newVal: any, oldVal: any) => {
    filteredFormType.value = filterFormType(props.formType, newVal.menu_type);
  };
</script>

<template>
  <a-modal
    title="新增菜单"
    :visible="visible"
    @ok="handleSubmit"
    title-align="start"
    :ok-loading="loading"
    @cancel="
      () => {
        emit('update:visible', false);
      }
    "
  >
    <pro-form
      v-if="isShow"
      ref="formRef"
      hiddenBtn
      :form-json="filteredFormType"
      :form-data="formData"
      @formModelChange="handleFormModelChange"
    ></pro-form>
  </a-modal>
</template>

<style scoped lang="less">
  :deep(.arco-select-view-inner) {
    text-align: left !important;
  }
</style>
